package com.weiyi.chat.presenter;

import android.app.Activity;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.weiyi.chat.api.IUserInfo;
import com.weiyi.chat.api.IUserList;
import com.weiyi.chat.common.Constant;
import com.weiyi.chat.common.MyStringCallBack;
import com.weiyi.chat.domain.AllUserInfo;
import com.weiyi.chat.domain.UserList;
import com.weiyi.chat.ui.activity.UserProfileActivity;
import com.weiyi.chat.ui.fragment.MessageFragment;
import com.weiyi.chat.utils.MyReusouce;
import com.weiyi.chat.utils.PreferenceUtil;
import com.weiyi.chat.utils.ToastUtil;
import com.zhy.http.okhttp.OkHttpUtils;

import org.json.JSONException;
import org.json.JSONObject;

import java.io.File;

/**
 * Created by Lee on 2017/8/24 0024.
 */

public class UserInfoPresenter implements IUserInfo.presenter {


    private UserProfileActivity mView;

    public UserInfoPresenter(UserProfileActivity userProfileActivity) {
        mView = userProfileActivity;
    }

    @Override
    public void updataAcvtar(File file, String token, String uid) {
        OkHttpUtils.post()
                .url(MyReusouce.UPDATA_AVATAR_URL)
                .addFile("avatar", file.getName(), file)
                .addParams("token", token)
                .addParams("bid", uid)
                .build()
                .execute(new MyStringCallBack(mView) {
                    @Override
                    public void onRequestSucess(String response) {
                        try {
                            JSONObject jsonObject = new JSONObject(response);
                            JSONObject body = jsonObject.getJSONObject("body");
                            JSONObject data = body.getJSONObject("data");
                            String originUrl = data.getString("avatar");
                            mView.onUpdataAvarar(originUrl);
                        } catch (JSONException e) {
                            e.printStackTrace();
                            ToastUtil.showTextToast(mView, "数据异常");
                        }
                    }

                    @Override
                    public void onRequstFail(String errorInfo) {
                        mView.showError(errorInfo);
                    }
                });
    }

    @Override
    public void updataNickName(String new_nick, String intro, String tel, String token) {

        OkHttpUtils.post()
                .url(MyReusouce.UPDATA_USER_INFO_URL)
                .addParams("bid", PreferenceUtil.getValue(Constant.SP_BUS_ID, mView))
                .addParams("name", new_nick)
                .addParams("intro", TextUtils.isEmpty(intro) ? "暂无" : intro)
                .addParams("tel", TextUtils.isEmpty(tel) ? "暂无" : tel)
                .addParams("token", token)
                .build()
                .execute(new MyStringCallBack(mView) {
                    @Override
                    public void onRequestSucess(String response) {
                        mView.onUpdataNick();
                    }

                    @Override
                    public void onRequstFail(String errorInfo) {
                        mView.showError(errorInfo);
                    }
                });
    }

    @Override
    public void getUserInfo(String user_id) {
        OkHttpUtils.post()
                .url(MyReusouce.GET_INFO_URL)
                .addParams("token", PreferenceUtil.getValue(Constant.SP_TOKEN, mView))
                .addParams("uid", user_id)
                .addParams("submit", "1")
                .build()
                .execute(new MyStringCallBack(mView) {
                    @Override
                    public void onRequestSucess(String response) {
                        Gson gson = new Gson();
                        AllUserInfo allUserInfo = gson.fromJson(response, AllUserInfo.class);
                        mView.onGetUserInfoSuccess(allUserInfo.getBody());
                    }

                    @Override
                    public void onRequstFail(String errorInfo) {
                        mView.showError(errorInfo);
                    }
                });
    }

    @Override
    public void loadingDatasFromCache() {

    }
}
